/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * author Svetlana Shikhutskaya
 */

var temp=true;
function showChangePassword(){
    document.getElementById("change-user-password").style.display='block';
}

function hideChangePassword(){
    document.getElementById("change-user-password").style.display='none';
}

function switchDivMode(){
    if(temp){
        document.getElementById("arrow").innerHTML='&#9660';
        showChangePassword();
        temp=false;
    }
    else{
        document.getElementById("arrow").innerHTML='&#9658';
        hideChangePassword();
        temp=true;
    }
}

function checkInfo(){
    var f_name=checkFName();
    var l_name=checkLName();
    var address=checkAddress();
    var email=document.getElementById("e-mail").value;
    var temp=validateEmail(email);
    if(!f_name || !l_name || !address){
        document.getElementById('ok').disabled=true;
//        document.getElementById('warning').innerHTML='Fill all the fields';
    }
    else{
        document.getElementById('warning').innerHTML='';
        if(email=='' || temp){
            document.getElementById('ok').disabled=false;
            document.getElementById('warningEmail').innerHTML='';
        }
        else{
            document.getElementById('ok').disabled=true;
            document.getElementById('warningEmail').innerHTML='Wrong email';
        }
    }
    
}

function matchPasswords(new_p, conf){
    if(new_p==conf){
        document.getElementById('save').disabled=false;
        document.getElementById('warningConfirm').innerHTML='';
    }
    else{
        document.getElementById('save').disabled=true;
        document.getElementById('warningConfirm').innerHTML='Passwords do not match';
    }
}

function checkPasswords(){
    var old_p=document.getElementById("old-password").value;
    var new_p=document.getElementById("new-password").value;
    var conf=document.getElementById("confirmation").value;
    if(old_p=='' || new_p == '' || conf==''){
        document.getElementById('save').disabled=true;
        document.getElementById('warningPassword').innerHTML='Fill all the fields';
        matchPasswords(new_p, conf);
    }
    else{
        document.getElementById('warningPassword').innerHTML='';
        matchPasswords(new_p, conf);
    }
}

function validateEmail(str){
    var regex=new RegExp("^[A-Za-z0-9.%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,4}$");
    return regex.test(str);
}

$(function(){
    $(document).on('click', '#arrow', function(e){
        e.preventDefault();
        switchDivMode();
    })
})

$(function(){
    $(document).on('click', '#btn-change-password', function(e){
        e.preventDefault();
        switchDivMode();
    })
})

$(function(){
    $(document).on('submit','.js-edit-user-info',function(e){
        e.preventDefault();
        var fname=document.getElementById("First_Name").value;
        var lname=document.getElementById("Last_Name").value;
        var address=document.getElementById("Address").value;
        if(fname!="" && lname!="" && address !=""){
            document.getElementById("warning").innerHTML='';
            $.post('EditUserInfo',{
                first_name:fname,
                last_name:lname,
                email:document.getElementById("e-mail").value,
                address:address
            },function(result){
                alert('Your personal information was successfully changed');
                window.location.reload();
            })
        }
        else{
            document.getElementById("warning").innerHTML='Fill all the fields';
        }
    })
})

$(function(){
    $(document).on('click','#save',function(e){
        e.preventDefault();
        var old_p=document.getElementById("old-password").value;
        var new_p=document.getElementById("new-password").value;
        var conf=document.getElementById("confirmation").value;
        if(new_p==conf){
            document.getElementById('warningConfirm').innerHTML='';
            $.post('EditUserPassword',{
                old_password:old_p,
                new_password:new_p
            },function(result){
                if(result=='true'){
                    window.location.reload();
                }
                else{
                    document.getElementById('warningPassword').innerHTML='Old password is wrong';
                }
            })
        }
        else{
            document.getElementById('save').disabled=true;
            document.getElementById('warningConfirm').innerHTML='';
        }
    })
})